#include"string.h"
namespace zhao
{
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n+1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}
	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		_str[_size] = c;
		++_size;
		_str[_size] = '\0';

	}

	string& string::append(const char* s)
	{
		size_t len = strlen(s);
		if (len + _size > _capacity)
			reserve(len + _size > 2 * _capacity ? len + _size : 2 * _capacity);
			strcpy(_str + _size, s);
			_size += len;
		return *this;
	}

	string& string::operator+= (const char* s)
	{
		return append(s);
	}

	void string::insert(size_t pos, const char* s)
	{
		size_t len = strlen(s);
		if (len + _size > _capacity)
			reserve(len + _size > 2 * _capacity ? len + _size: 2 * _capacity);
		size_t end = _size + len;
		//size_t end = pos + len;
		while (end>pos+len-1)
		{
			_str[end] = _str[end - len];
			--end;
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = s[i];
		}

		_size += len;
		
	}

}